<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends BIT_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */

	public function __construct()
	{
		parent::__construct();
		$this->header_views = array('common/header');
		$this->footer_views = array('common/footer');
	}
	public function index()
	{
		$this->v('pages/home');
	}
	
	public function search()
	{
		$data = array();
		$viewtoload = "pages/home";

		if($this->input->get("what") || $this->input->get('where'))
		{
			$what = $this->input->get("what");
			$where = $this->input->get("where");
			
			$match = $what ? "+".$what : "";
			$match .= $where ? " +". $where : "";	

			$query = "SELECT c.name, c.phone, c.email, c.homepage, c.orgnr, c.address, c.slug, branches.name AS branchname FROM `company` AS c JOIN branches ON (branches.id = c.branch) WHERE (MATCH(c.`name`, `address`, `search_tags`, `orgnr`) AGAINST ('".$match."'IN BOOLEAN MODE))";

			if($what) $query .= " OR `branch` IN (SELECT `id` FROM `branches` WHERE `name` LIKE '%".$what."%');";

			$this->view_data['query'] = $query;
			$this->view_data['results'] = $this->db->query($query)->result();
			$this->view_data['hits'] = count($this->view_data['results']);

			$this->view_data["title"] = "Din sökning på ". $what;
			
			$this->view_data["what"] = $what;
			$this->view_data["where"] = $where;	
			$viewtoload = "pages/results";
		}

		$this->v($viewtoload);
	}

	public function contact(){
		$mail_sent = false;

		# Loading configuration file mail_config.php
		$this->config->load('mail_config',true);

        if ($this->input->post('q'))
        {
        	$this->load->helper(array('form', 'url'));
			$this->load->library('form_validation');
			$form_rules = array(
				array(
					'field' => 'from',
					'label' => 'epost',
					'rules' => 'trim|valid_email|required'
				),
				array(
					'field' => 'name',
					'label' => 'namn',
					'rules' => 'min_length[3]|required'
				),
				array(
					'field' => 'subject',
					'label' => 'ämne',
					'rules' => 'min_length[3]|required'
				),
				array(
					'field' => 'message',
					'label' => 'meddelande',
					'rules' => 'min_length[20]|required'
				)
			);
			$this->form_validation->set_message('required', 'Du måste fylla i %sfältet');
			$this->form_validation->set_message('min_length', '%s får inte vara kortare än %s tecken');
			$this->form_validation->set_message('valid_email', 'Epostadressen är ogiltig');

			$this->form_validation->set_rules($form_rules);
			
			if($this->form_validation->run() == TRUE)
			{
	            # Loading email library of Codeigniter
	            $this->load->library('email');
	            # Setting email address and name of the person sending the email
	            $this->email->from($this->input->post('from'),$this->input->post('name'));
	            # Setting email address of the recipient
	            $this->email->to($this->config->item('to','mail_config'));
	            # Setting email subject
	            $this->email->subject($this->input->post('subject'));
	            # Setting email message body
	            $this->email->message($this->input->post('message',true));
	            # If mail sending successful
	            if ($this->email->send())
	            {
	                # If $mail_sent = true; it will show a success message.
	                $mail_sent = true;
	            }
	        }
        }
        # Showing Contact Form
        $this->view_data['mail_sent'] = $mail_sent;
        $this->view_data['mailaddress'] = $this->config->item('to','mail_config');
        $this->v('pages/contact');
	}

	public function pages($slug){
		$this->view_data['page'] = $this->pages->getPageBySlug($slug);
		$this->v('pages/about');
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */